package day16.xianchengdemo.xianchengapi;

/**
 * 设置名字，改名字，拿到当前线程对象，线程放缓，线程优先（插队）
 *
 *run是任务方法，start是启动方法
 *
 * */
public class demo1
{
    public static void main(String[] args) {
        Thread1 t1=new Thread1("子线程一");//一般不改名字，默认就是Thread-索引
        t1.setName("一号子线程");
        t1.start();

        Thread1 t2=new Thread1("二号子线程");//一般不改名字，默认就是Thread-索引
        t2.start();

        Thread.currentThread().setName("主线程");//把当前线程改名字
        System.out.println(Thread.currentThread().getName());
        System.out.println(t1.getName());
        System.out.println(t2.getName());
        Thread.currentThread();//当前线程对象

//        try {                            你把主线程调sleep方法，你把主线程放缓不就相当于给子线程让路，让他插队了
//            Thread.currentThread().sleep(1000);//放缓
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }

        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            if(i==0)
            {
                try {
                    t1.join();//让当前线程优先完成
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

        }


    }
}
class Thread1 extends Thread
{
    public Thread1()
    {

    }
    public Thread1(String name)
    {
        super(name);
    }

    @Override
    public void run()
    {                         //拿到当前线程对象，那就看哪个线程调他了，就拿到谁
        System.out.println(Thread.currentThread().getName() + ":的输出");
        System.out.println("测试子线程");
        System.out.println("测试子线程");
        try {
            Thread.currentThread().sleep(10000);//放缓   优先让你完成，但是你突然又放缓了，cpu就让其他线程继续先做
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("测试子线程:"+Thread.currentThread().getName());
        System.out.println("测试子线程:"+Thread.currentThread().getName());
    }
}

